package com.clz.latte.ec.main.sort.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.clz.latte.delegate.LatteDaleGate;
import com.clz.latte.ec.R;
import com.clz.latte.ec.R2;
import com.clz.latte.ec.entity.sort.VerticalListBean;
import com.clz.latte.ec.main.sort.SortDelegate;
import com.clz.latte.ec.main.sort.VerticalListRecAdapter;
import com.clz.latte.net.RestClient;
import com.clz.latte.net.callback.ISuccess;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/4/8.
 */

public class VerticalListDelegate extends LatteDaleGate {
    @BindView(R2.id.rv_vertical_menu_list)
    RecyclerView mRecycleMenuList;
    private VerticalListRecAdapter mAdapter = null;
    private List<VerticalListBean> mData = null;

    @Override
    public Object setLayOut() {
        return R.layout.delegate_verticlal_list;
    }

    private void initRecycle() {
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecycleMenuList.setLayoutManager(layoutManager);
        final SortDelegate parentDaleGate = getParentDaleGate();
        mAdapter = new VerticalListRecAdapter(R.layout.item_vertical_menu_list, parentDaleGate);
        mRecycleMenuList.setAdapter(mAdapter);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initRecycle();
        getMenuData();
    }

    private void getMenuData() {
        RestClient.builder()
                .url("sort_list.php")
                .loader(getContext())
                .sucess(new ISuccess() {
                    @Override
                    public void onSucess(String response) {
                        JSONObject jsonObject = JSONObject.parseObject(response);
                        final JSONObject json = jsonObject.getJSONObject("data");
                        Gson gson = new Gson();
                        mData = gson.fromJson(json.getJSONArray("list").toJSONString(), new TypeToken<ArrayList<VerticalListBean>>(){}.getType());
                        mAdapter.setNewData(mData);
                    }
                })
                .build()
                .get();
    }
}
